//
//  OwnerAsView.m
//  MonitoringNetwork
//
//  Created by admin on 2017/12/19.
//  Copyright © 2017年 wtmcxlm. All rights reserved.
//

#import "OwnerAsView.h"
#import "OwnerModel.h"
#import "OwnerChilderModel.h"

#import "UIButton+BaseAttributes.h"
#import "UILabel+BasicAttributes.h"


@interface OwnerAsView()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (nonatomic,strong)UIView *dimView;
@property (nonatomic,strong)UIView *baseView;
@property (nonatomic,strong)UILabel *titleLabel;
@property (nonatomic,strong)UIPickerView *pickView;

@property (nonatomic,strong)NSArray *ownerChilderArr;
@property (nonatomic,strong)NSArray *ownerChilderdeChilderArr;
@property (nonatomic,strong)OwnerModel *returnModel;

@property (nonatomic,assign)NSInteger index1;
@property (nonatomic,assign)NSInteger index2;
@property (nonatomic,assign)NSInteger index3;

@property (nonatomic,assign)NSInteger index4;
@property (nonatomic,assign)NSInteger index5;
@property (nonatomic,assign)NSInteger index6;


@property (nonatomic,assign)NSInteger tempIndex;
@property (nonatomic,assign)BOOL firstLoad;

@property (nonatomic,copy)NSString *ownerText;
@property (nonatomic,copy)NSString *ownerId;


//@property (nonatomic,strong)UIView *lineView;

@end

@implementation OwnerAsView

- (UIPickerView *)pickView
{
    if (!_pickView) {
        _pickView = [[UIPickerView alloc] init];
        _pickView.dataSource = self;
        _pickView.delegate = self;
    }
    return _pickView;
}
- (UIView *)dimView
{
    if(!_dimView) {
        _dimView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        _dimView.alpha = 0.0f;
        _dimView.backgroundColor = [UIColor lightGrayColor];
        [self addSubview:_dimView];
    }
    return _dimView;
}
- (UIView *)baseView
{
    if (!_baseView) {
        _baseView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight, kScreenWidth, kScreenHeight*0.4)];
        _baseView.alpha = 1.0f;
        _baseView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_baseView];
    }
    return _baseView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self setUpUI];
    }
    return self;
}
- (void)setUpUI
{
    _firstLoad = YES;
    [UIView animateWithDuration:0.3 animations:^{
        self.baseView.frame = CGRectMake(0, kScreenHeight - kScreenHeight*0.4, kScreenWidth, kScreenHeight*0.4);
        self.dimView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - kScreenHeight*0.4);
//        self.lineView.frame = CGRectMake(kScreenWidth*0.9, kScreenHeight - kScreenHeight*0.2, kScreenWidth*0.1, kScreenHeight);
        self.dimView.alpha = 0.20f;
    }];
    UIButton *cancelBtn = [UIButton bttonWithTitle:@"取消" andNormalTitleColor:[UIColor colorWithRed:0.33f green:0.59f blue:0.87f alpha:1.00f] selectColor:[UIColor colorWithRed:0.33f green:0.59f blue:0.87f alpha:1.00f] titleFont:BigFontSize];
    [cancelBtn addTarget:self action:@selector(buttonDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [_baseView addSubview:cancelBtn];
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(_baseView);
        make.height.mas_equalTo(40);
        make.width.mas_equalTo(100);
    }];
    
    
    
    UIButton *confirmBtn = [UIButton bttonWithTitle:@"完成" andNormalTitleColor:[UIColor colorWithRed:0.33f green:0.59f blue:0.87f alpha:1.00f] selectColor:[UIColor colorWithRed:0.33f green:0.59f blue:0.87f alpha:1.00f] titleFont:BigFontSize];
    [confirmBtn addTarget:self action:@selector(buttonDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [_baseView addSubview:confirmBtn];
    [confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.mas_equalTo(_baseView);
        make.height.mas_equalTo(40);
        make.width.mas_equalTo(100);
    }];
    
    _titleLabel = [UILabel LabelWithFont:BigFontSize andColor:[UIColor colorWithRed:0.38f green:0.38f blue:0.38f alpha:1.00f] andTextAlignment:center];
    _titleLabel.text = self.title;
    [_baseView addSubview:_titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(40);
        make.left.mas_equalTo(cancelBtn.mas_right);
        make.right.mas_equalTo(confirmBtn.mas_left);
        make.centerY.mas_equalTo(confirmBtn);
    }];
    
    [_baseView addSubview:self.pickView];
    [_pickView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(_baseView);
        make.top.mas_equalTo(cancelBtn.mas_bottom);
    }];
}


- (void)layoutSubviews
{
    [super layoutSubviews];
     _titleLabel.text = _title;
    
    
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self hidde];
}
- (void)show
{
    
    self.hidden = NO;
    [UIView animateWithDuration:0.3 animations:^{
        self.baseView.frame = CGRectMake(0, kScreenHeight - kScreenHeight*0.4, kScreenWidth, kScreenHeight*0.4);
        self.dimView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - kScreenHeight*0.4);
        self.dimView.alpha = 0.20f;
    }];
    
    
}
- (void)hidde
{
    if (_viewHadRemoveFromeSup) {
        _viewHadRemoveFromeSup();
    }
    
    [UIView animateWithDuration:0.3 animations:^{
        self.baseView.frame = CGRectMake(0, kScreenHeight, kScreenWidth, kScreenHeight*0.4);
        self.dimView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
        self.dimView.alpha = 0.0f;
    } completion:^(BOOL finished) {
        self.hidden = YES;
    }];
}
- (void)selfRemoveToSub
{
   
}
- (void)buttonDidClick:(UIButton *)sender
{
    [self hidde];
    if ([sender.titleLabel.text isEqualToString:@"取消"]) {
//        [self.pickView reloadAllComponents];
//        if (_resultModel) {
//            _resultModel(_ownerText,_ownerId);
//        }
    }else{
        _index4 = _index1;
        _index5 = _index2;
        _index6 = _index3;
        OwnerModel *tempModel = [OwnerModel new];
        
        if (self.ownerArr.count == _index1||self.ownerArr.count == 0) {//第一级全部
            tempModel.text = @"全部";
            tempModel.userId = @"";
        }else {//第一级下级
            if (self.ownerChilderArr.count == 0) {//无第二级
                
                NSDictionary *firstDic = self.ownerArr[_index1];
                tempModel.text = [NSString stringWithFormat:@"%@-全部",firstDic[@"text"]];
                tempModel.userId = [NSString stringWithFormat:@"%@",firstDic[@"id"]];
                
            }else if (self.ownerChilderArr.count == _index2){//第二级全部
                NSDictionary *firstDic = self.ownerArr[_index1];
                NSMutableString *allAreaString = [NSMutableString string];
                [allAreaString appendFormat:@"%@,",firstDic[@"id"]];
                for (int i=0; i<self.ownerChilderArr.count; i++) {
                    NSDictionary *dic = self.ownerChilderArr[i];
                    if (i == self.ownerChilderArr.count-1) {
                        [allAreaString appendFormat:@"%@",dic[@"id"]];
                    }else{
                        [allAreaString appendFormat:@"%@,",dic[@"id"]];
                    }
                }
                tempModel.text = [NSString stringWithFormat:@"%@-全部",firstDic[@"text"]];
                tempModel.userId = allAreaString.copy;
            }else{//第二级下级
                if (self.ownerChilderdeChilderArr.count == 0) {//无第三级
                    NSDictionary *firstDic = self.ownerArr[_index1];
                    NSDictionary *secondDic = self.ownerChilderArr[_index2];
                    tempModel.text = [NSString stringWithFormat:@"%@-%@-全部",firstDic[@"text"],secondDic[@"text"]];
                    tempModel.userId = [NSString stringWithFormat:@"%@",secondDic[@"id"]];
                }else if (self.ownerChilderdeChilderArr.count == _index3){//第三级全部
                    NSDictionary *firstDic = self.ownerArr[_index1];
                    NSDictionary *secondDic = self.ownerChilderArr[_index2];
                    NSMutableString *allAreaString = [NSMutableString string];
                    [allAreaString appendFormat:@"%@,",secondDic[@"id"]];
                    for (int i=0; i<self.ownerChilderdeChilderArr.count; i++) {
                        NSDictionary *dic = self.ownerChilderdeChilderArr[i];
                        if (i == self.ownerChilderdeChilderArr.count-1) {
                            [allAreaString appendFormat:@"%@",dic[@"id"]];
                        }else{
                            [allAreaString appendFormat:@"%@,",dic[@"id"]];
                        }
                    }
                    tempModel.text = [NSString stringWithFormat:@"%@-%@-全部",firstDic[@"text"],secondDic[@"text"]];
                    tempModel.userId = allAreaString.copy;
                }else{//城市级
                    NSDictionary *firstDic = self.ownerArr[_index1];
                    NSDictionary *secondDic = self.ownerChilderArr[_index2];
                    NSDictionary *thirdDic = self.ownerChilderdeChilderArr[_index3];
                    NSArray *fourthArr = thirdDic[@"children"];//城市下级
                    
                    
                    NSMutableString *allAreaString = [NSMutableString string];
                    
                    for (int i=0; i<fourthArr.count; i++) {
                        NSDictionary *dic = fourthArr[i];
                        [allAreaString appendFormat:@"%@,",dic[@"id"]];
                    }
//                    [allAreaString appendFormat:@"%@,",secondDic[@"id"]];
                    [allAreaString appendFormat:@"%@",thirdDic[@"id"]];
                    tempModel.text = [NSString stringWithFormat:@"%@-%@-%@",firstDic[@"text"],secondDic[@"text"],thirdDic[@"text"]];
                    tempModel.userId = allAreaString.copy;
                    
                }
               
            }
        }
        
        _returnModel = tempModel;
        if (_resultModel) {
            _resultModel(tempModel.text,tempModel.userId);
        }
    }
}

#pragma mark - UIPickView 代理 数据源
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:[UIFont boldSystemFontOfSize:15]];
    }
    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView
{
    return 3;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    
    if (component == 0) {
        if (self.ownerArr.count) {
            return self.ownerArr.count +1;
        }else{
            return 0;
        }
        
    }else if(component == 1) {
        return self.ownerChilderArr.count+1;
    }else{
        return self.ownerChilderdeChilderArr.count+1;
    }
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    
    
    if (component == 0) {
        _index1 = row;
        if (row < self.ownerArr.count && self.ownerArr.count) {
            NSDictionary *fisrtDic = self.ownerArr[row];
            return fisrtDic[@"text"];
        }else if(row == self.ownerArr.count && self.ownerArr.count){
            return @"全部";
        }else{
             return @"";
        }
    }else if (component == 1){
        if (self.ownerChilderArr.count) {
            if (row == self.ownerChilderArr.count) {
                return @"全部";
            }else{
                NSDictionary *secondDic = self.ownerChilderArr[row];
                return secondDic[@"text"];
            }
        }else{
            return @"全部";
        }
    }else{
        if (self.ownerChilderdeChilderArr.count) {
            if (row == self.ownerChilderdeChilderArr.count) {
                return @"全部";
            }else{
                NSDictionary *thirdDic = self.ownerChilderdeChilderArr[row];
                return thirdDic[@"text"];
            }
        }else{
            return @"全部";
        }
    }
    
    
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    
    if (component == 0) {
        _index1 = row;
        if (_index1>self.ownerArr.count) {
            _index1 = self.ownerArr.count;
        }
        if (self.ownerArr.count == 0) {
            self.ownerChilderArr = [NSArray array];
        }else{
            if (self.ownerArr.count==row) {
                self.ownerChilderArr = [NSArray array];
            }else{
                NSDictionary *firstDic = self.ownerArr[_index1];
                if ([firstDic[@"children"] isKindOfClass:[NSString class]]) {
                    self.ownerChilderArr = [NSArray array];
                }else{
                    self.ownerChilderArr = (NSArray *)firstDic[@"children"];
                }
            }
            
        }
        self.ownerChilderdeChilderArr = [NSArray array];
        _index2 = self.ownerChilderArr.count;
        _index3 = self.ownerChilderdeChilderArr.count;
        [self.pickView reloadAllComponents];
        [self.pickView selectRow:_index2 inComponent:1 animated:YES];
        [self.pickView selectRow:_index3 inComponent:2 animated:YES];
        
    }
    
    if (component == 1) {
        _index2 = row;
        if (_index2>self.ownerChilderArr.count) {
            _index2 = self.ownerChilderArr.count;
        }
        if (0 == self.ownerChilderArr.count) {
            self.ownerChilderdeChilderArr = [NSArray array];
        }else{
            if (row == self.ownerChilderArr.count) {
                 self.ownerChilderdeChilderArr = [NSArray array];
            }else{
                NSDictionary *seconfDic = self.ownerChilderArr[_index2];
                if ([seconfDic[@"children"] isKindOfClass:[NSString class]]) {
                    self.ownerChilderdeChilderArr = [NSArray array];
                }else{
                     self.ownerChilderdeChilderArr = (NSArray *)seconfDic[@"children"];
                }
            }
        }
        _index3 = self.ownerChilderdeChilderArr.count;
        [self.pickView reloadAllComponents];
        [self.pickView selectRow:_index3 inComponent:2 animated:YES];
    }
    
    if (component == 2) {
        _index3 = row;
        if (_index3>self.ownerChilderdeChilderArr.count) {
            _index3 = self.ownerChilderdeChilderArr.count;
        }
    }
}

- (void)loadOwnerBottomViewWithOwnerArr:(NSArray *)arr
{
    if (_firstLoad) {
        _firstLoad = NO;
        _index1 = arr.count;
        _index2 = 0;
        _index3 = 0;
        _index4 = _index1;
        _index5 = _index2;
        _index6 = _index3;
        self.ownerChilderArr = [NSArray array];
        self.ownerChilderdeChilderArr = [NSArray array];
        _returnModel.text = @"全部";
        _returnModel.userId = @"";
    }
    
//    if (_resultModel) {
//        _resultModel(_returnModel.text,_returnModel.userId);
//    }
    [self.pickView reloadAllComponents];
    [self.pickView selectRow:_index4 inComponent:0 animated:YES];
    [self.pickView selectRow:_index5 inComponent:1 animated:YES];
    [self.pickView selectRow:_index6 inComponent:2 animated:YES];
}

@end
